﻿#include "userrelation.h"
#include "tools.h"

UserRelation::UserRelation(QObject *parent) : QObject(parent)
{

}

void UserRelation::loadFromJsonObject(QJsonObject obj)
{
    id = obj.value("id").toInt();
    sourceUsername = obj.value("source_username").toString();
    sourceCompanyId = obj.value("source_company_id").toString();
    targetUsername = obj.value("target_username").toString();
    targetCompanyId = obj.value("target_company_id").toString();
    nickName = obj.value("nick_name").toString();
    nickNamePinyin = obj.value("nick_name_pinyin").toString();
    contact = obj.value("contact").toString();
    contactPinyin = obj.value("contact_pinyin").toString();
    tel = obj.value("tel").toString();
    type = obj.value("type").toInt();
    isPlatform = obj.value("is_platform").toInt();
    ticketIn = obj.value("ticket_in").toDouble();
    ticketOut = obj.value("ticket_out").toDouble();
    moneyIn = obj.value("money_in").toDouble();
    moneyOut = obj.value("money_out").toDouble();
    preTicketIn = obj.value("pre_ticket_in").toDouble();
    preTicketOut = obj.value("pre_ticket_out").toDouble();
    preMoneyIn = obj.value("pre_money_in").toDouble();
    preMoneyOut = obj.value("pre_money_out").toDouble();
    hasSubAccount = obj.value("has_sub_account").toInt();
    hasTicketHistory = obj.value("has_ticket_history").toInt();
    credit = obj.value("credit").toInt();
    remark = obj.value("remark").toString();
    primaryDirectory = obj.value("primary_directory").toString();
    secondaryDirectory = obj.value("secondary_directory").toString();

//    Tools::cout("id:"+QString("%1").arg(id));
//    Tools::cout("sourceUsername:"+sourceUsername);
//    Tools::cout("sourceCompanyId:"+sourceCompanyId);
//    Tools::cout("targetUsername:"+targetUsername);
//    Tools::cout("targetCompanyId:"+targetCompanyId);
//    Tools::cout("nickName:"+nickName);
//    Tools::cout("nickNamePinyin:"+nickNamePinyin);
//    Tools::cout("contact:"+contact);
//    Tools::cout("contactPinyin:"+contactPinyin);
//    Tools::cout("tel:"+tel);
//    Tools::cout("type:"+QString("%1").arg(type));
//    Tools::cout("isPlatform:"+QString("%1").arg(isPlatform));
//    Tools::cout("ticketIn:"+QString("%1").arg(ticketIn));
//    Tools::cout("ticketOut:"+QString("%1").arg(ticketOut));
//    Tools::cout("moneyIn:"+QString("%1").arg(moneyIn));
//    Tools::cout("moneyOut:"+QString("%1").arg(moneyOut));
//    Tools::cout("preTicketIn:"+QString("%1").arg(preTicketIn));
//    Tools::cout("preTicketOut:"+QString("%1").arg(preTicketOut));
//    Tools::cout("preMoneyIn:"+QString("%1").arg(preMoneyIn));
//    Tools::cout("preMoneyOut:"+QString("%1").arg(preMoneyOut));
//    Tools::cout("hasSubAccount:"+QString("%1").arg(hasSubAccount));
//    Tools::cout("hasTicketHistory:"+QString("%1").arg(hasTicketHistory));
//    Tools::cout("credit:"+QString("%1").arg(credit));
//    Tools::cout("remark:"+remark);
//    Tools::cout("primaryDirectory:"+primaryDirectory);
//    Tools::cout("secondaryDirectory:"+secondaryDirectory);
}

void UserRelation::loadFromSqlQuery(QSqlQuery query)
{
    id = query.value(0).toInt();
    sourceUsername = query.value(1).toString();
    sourceCompanyId = query.value(2).toString();
    targetUsername = query.value(3).toString();
    targetCompanyId = query.value(4).toString();
    nickName = query.value(5).toString();
    nickNamePinyin = query.value(6).toString();
    contact = query.value(7).toString();
    contactPinyin = query.value(8).toString();
    tel = query.value(9).toString();
    type = query.value(10).toInt();
    isPlatform = query.value(11).toInt();
    ticketIn = query.value(12).toDouble();
    ticketOut = query.value(13).toDouble();
    moneyIn = query.value(14).toDouble();
    moneyOut = query.value(15).toDouble();
    preTicketIn = query.value(16).toDouble();
    preTicketOut = query.value(17).toDouble();
    preMoneyIn = query.value(18).toDouble();
    preMoneyOut = query.value(19).toDouble();
    hasSubAccount = query.value(20).toInt();
    hasTicketHistory = query.value(21).toInt();
    credit = query.value(22).toInt();
    remark = query.value(23).toString();
    primaryDirectory = query.value(24).toString();
    secondaryDirectory = query.value(25).toString();
}

QString UserRelation::toJson()
{
    QString jsonStr = "{";

    jsonStr.append("\"nick_name\":"+Tools::toJsonStr(nickName)+",");
    jsonStr.append("\"contact\":"+Tools::toJsonStr(contact)+",");
    jsonStr.append("\"tel\":"+Tools::toJsonStr(tel)+",");
    jsonStr.append("\"remark\":"+Tools::toJsonStr(remark)+",");
    jsonStr.append("\"primary_directory\":"+Tools::toJsonStr(primaryDirectory)+",");
    jsonStr.append("\"secondary_directory\":"+Tools::toJsonStr(secondaryDirectory)+",");

    jsonStr.append("}");

    return jsonStr;
}
